package com.yami.shop.platform.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yami.shop.bean.dto.BlockchainShopDTO;
import com.yami.shop.bean.event.BlockchainShopEvent;
import com.yami.shop.bean.model.BlockchainShop;
import com.yami.shop.bean.model.ShopChainApply;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.bean.vo.BlockchainShopVO;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.BlockchainShopService;
import com.yami.shop.service.ShopChainApplyService;
import com.yami.shop.service.ShopDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author lth
 * @Date 2021/8/23 9:44
 */
@RestController
@RequestMapping("/platform/blockchainShop")
@Tag(name = "签约链平台相关接口")
public class BlockchainShopController {

    @Autowired
    private BlockchainShopService blockchainShopService;

    @Autowired
    private ShopChainApplyService shopChainApplyService;

    @Autowired
    private ShopDetailService shopDetailService;

    @Autowired
    private ApplicationContext applicationContext;
    @GetMapping("/page")
    @Operation(summary =  "分页查询", description = "分页查询")
    public ServerResponseEntity<IPage<BlockchainShopVO>> getShopAuditingPage(PageParam<BlockchainShopVO> page, @ParameterObject BlockchainShopDTO blockchainShopDTO) {
        blockchainShopDTO.setBlockchainShopStatus(0);
        return ServerResponseEntity.success(blockchainShopService.pageSigningInfo(page, blockchainShopDTO));
    }

    @DeleteMapping
    @Operation(summary = "删除签约链", description = "删除签约链")
    @Parameters(value = {@Parameter(name = "shopId", description = "店铺id"), @Parameter(name = "blockchainId", description = "链平台id")})
    public ServerResponseEntity<Void> delete(@RequestParam("shopId") Long shopId, @RequestParam("blockchainId") Long blockchainId) {

        BlockchainShop blockchainShop = blockchainShopService.getOne(new LambdaUpdateWrapper<BlockchainShop>().eq(BlockchainShop::getShopId, shopId).eq(BlockchainShop::getBlockchainId, blockchainId));
        // 删除对应链的铸币能力申请记
        shopChainApplyService.update(new LambdaUpdateWrapper<ShopChainApply>().set(ShopChainApply::getStatus, 0).eq(ShopChainApply::getShopId, shopId).eq(ShopChainApply::getChainCode,blockchainShop.getBlockchainCode()));
        blockchainShopService.delete(shopId, blockchainId);
        return ServerResponseEntity.success();
    }

    @PutMapping("updateDiscount")
    @Operation(summary = "更新能量充值折扣率", description = "更新能量充值折扣率")
    @Parameters(value = {@Parameter(name = "shopId", description = "店铺id"), @Parameter(name = "discount", description = "能量充值折扣率")})
    public ServerResponseEntity<Void> updateRate(@RequestParam("shopId") Long shopId, @RequestParam("blockchainId") Long blockchainId, @RequestParam(name = "discount", required = false) Double discount) {
        blockchainShopService.updateDiscount(shopId, blockchainId, discount);
        return ServerResponseEntity.success();
    }

    @PostMapping("/auditShopChain")
    @Operation(summary = "审核商铺申请链信息", description = "审核商铺申请链信息")
    public ServerResponseEntity<Void> auditShopChain(@RequestBody BlockchainShopDTO blockchainShopDTO) {
        if(blockchainShopDTO.getBlockchainShopStatus() != 1 && blockchainShopDTO.getBlockchainShopStatus() != -1){
            return ServerResponseEntity.showFailMsg("参数异常");
        }
        BlockchainShop dbShopchain = blockchainShopService.getById(blockchainShopDTO.getBlockchainShopId());
        if(dbShopchain.getStatus() != 0){
            return ServerResponseEntity.showFailMsg("状态数据异常");
        }
        String auditReason = blockchainShopDTO.getAuditReason();
        if(blockchainShopDTO.getBlockchainShopStatus() == -1 && StrUtil.isEmpty(auditReason)){
            return ServerResponseEntity.showFailMsg("请填写驳回原因");
        }
        blockchainShopService.update(Wrappers.lambdaUpdate(BlockchainShop.class).eq(BlockchainShop::getBlockchainShopId,blockchainShopDTO.getBlockchainShopId())
                .set(BlockchainShop::getStatus,blockchainShopDTO.getBlockchainShopStatus())
                .set(BlockchainShop::getAuditReason,auditReason).set(BlockchainShop::getAuditTime, new Date()));

        List<Long> shopIds = new ArrayList<>();
        shopIds.add(dbShopchain.getShopId());
        blockchainShopService.removeCacheByShopIds(shopIds);
        ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(dbShopchain.getShopId());
        if(StrUtil.isEmpty(shopDetail.getApiUser())){
            // api平台补api鉴权账号和api鉴权密码
            List<BlockchainShop> addBlockchainList = new ArrayList<>();
            BlockchainShop blockchainShop = new BlockchainShop();
            blockchainShop.setShopCode(shopDetail.getChannelCode());
            blockchainShop.setIsCoin(0);
            blockchainShop.setIsSynchronization(0);
            blockchainShop.setCreateTime(new Date());
            blockchainShop.setApplyTime(new Date());
            addBlockchainList.add(blockchainShop);
            applicationContext.publishEvent(new BlockchainShopEvent(addBlockchainList, dbShopchain.getShopId(), shopDetail));
        }
        return ServerResponseEntity.success();
    }
}
